Imports System

''' <summary>
''' Methods for security utilities.
''' </summary>
Public Class SecurityUtil

#Region "Private constructor"

	Private Sub New()
		' Can't create instance!
	End Sub

#End Region

#Region "Variables"

	Private Const CHARACTERS As String = "ABCDEFGHIJKLMNOPQRSTUVXZWYabcdefghijklmnopqrstuvxzwy0123456789"

#End Region

#Region "Random password"

	''' <summary>
	''' Generates a random password with the specified length.
	''' </summary>
	''' <returns>Random password as String.</returns>
	Public Shared Function GeneratePassword(ByVal length As Integer) As String

		Return GeneratePassword(length, length, CHARACTERS)

	End Function

	''' <summary>
	''' Generates a random password with the specified minimum and maximum length and valid characters.
	''' </summary>
	''' <returns>Random password as String.</returns>
	Public Shared Function GeneratePassword(ByVal minLength As Integer, ByVal maxLength As Integer, ByVal validCharacters As String) As String

		Dim rnd As New Random
		Dim password As String = String.Empty
		Dim strLength As Integer = validCharacters.Length
		Dim length As Integer = rnd.Next(minLength, maxLength + 1)
		Dim i As Integer

		For i = 1 To length
			password &= validCharacters.Substring(rnd.Next(0, strLength), 1)
		Next

		Return password

	End Function

#End Region

End Class